package com.zlt.dao.Impl;

import com.zlt.dao.AdminDao;
import com.zlt.entity.Admin;
import com.zlt.entity.Company;
import com.zlt.entity.License;
import com.zlt.entity.Question;
import com.zlt.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public enum AdminDaoImpl implements AdminDao {
    INSTANCE;

    AdminDaoImpl() {
    }

    private final QueryRunner runner = new QueryRunner(JDBCUtil.getDataSource());

    @Override
    public Admin existedByUsername(String username) throws SQLException {
        String sql = "SELECT aid id,`password`,salt FROM `admin` WHERE username=?";
        return runner.query(sql, new BeanHandler<>(Admin.class), username);
    }

    @Override
    public List<Company> listCompany(Integer start, Integer end) throws SQLException {
        String sql = "SELECT cid,cname,address,legal_person legalPerson,contact,phone,state FROM company LIMIT ?,?";
        return runner.query(sql, new BeanListHandler<>(Company.class), start, end);
    }

    @Override
    public List<Company> listCompany(Integer start, Integer end, Integer state) throws SQLException {
        String sql = "SELECT cid,cname,address,legal_person legalPerson,contact,phone FROM company WHERE state=? LIMIT ?,?";
        return runner.query(sql, new BeanListHandler<>(Company.class), state, start, end);
    }

    @Override
    public Long countCompany(Integer state) throws SQLException {
        String sql = "SELECT COUNT(*) FROM company WHERE state=?";
        return runner.query(sql, new ScalarHandler<>(), state);
    }

    @Override
    public List<Question> listQuestions(Integer start, Integer end) throws SQLException {
        String sql = "SELECT id,uid,content,create_time createTime,state FROM `question` LIMIT ?,?";
        return runner.query(sql, new BeanListHandler<>(Question.class), start, end);
    }

    @Override
    public List<Question> listQuestions(Integer start, Integer end, Integer state) throws SQLException {
        String sql = "SELECT id,uid,content,create_time createTime FROM `question` WHERE state=? LIMIT ?,?";
        return runner.query(sql, new BeanListHandler<>(Question.class), state, start, end);
    }

    @Override
    public Long countQuestion(Integer state) throws SQLException {
        String sql = "SELECT COUNT(*) FROM `question` WHERE state=?";
        return runner.query(sql, new ScalarHandler<>(), state);
    }

    @Override
    public Integer updateCompanyState(Integer cid, Integer state) throws SQLException {
        String sql = "UPDATE company SET state=? WHERE cid=?";
        return runner.update(sql, state, cid);
    }

    @Override
    public List<License> selectLicencesByCid(Integer cid) throws SQLException {
        String sql = "SELECT lname,file FROM `licenses` WHERE cid=?";
        return runner.query(sql, new BeanListHandler<>(License.class), cid);
    }
}
